#!/bin/bash
#myapp              service of myapp
#添加chkconfig功能,chkconfig和description必须要加.运行级别2、3、4、5，20开机启动优先级，不能太小否则可能依赖的程序还没起来。90是关闭优先级。
#chkconfig: 2345 20 90                       
#description: Start stop restart status
#
#操作流程：
#1.chmod +x /path/my/myapp_service
#2.mv service /etc/init.d/myapp_service       #把该文件移到init.d目录下,要具有可执行权限.
#3.ls -sf /etc/init.d/myapp_service /etc/rc3.d/S100myapp_service
#4.chkconfig --add myapp_service              #开机自启myapp_service服务
#5.service myapp_service start                #启动myapp_service服务

#注意:服务启动,所有依赖都需要启动.服务停止,所有依赖都需要停止.

# Source function library.
. /etc/rc.d/init.d/functions

#myapp的绝对路径
PROC_MYAPP=/path/myapp

#watchdog绝对路径.通过watchdog来启动程序.
PROC_WATCHDOG=/path/watchdog

#服务脚本中无环境变量,所以要添加.解决中文乱码的问题.
#MYAPP_LANG=`echo $LANG`

#一个提示字符串
usage_str="See Usgage"

#命令执行后的返回值
RETVAL=0

#获取进程个数的函数,函数只能return返回数字，如果返回字符串用echo.
#需要一个参数
function GetProcNum() {
  if [ x"$1" = x ];then
    return 99
  fi
  ProcNum=`ps -ef | grep -w $1 | grep -v grep | wc -l`
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then #命令执行成功
    echo $ProcNum           #以字符串形式返回ProcNum
  else
    return 99
  fi
}

#获取进程ID的函数,函数只能return返回数字，如果返回字符串用echo.
#需要一个参数
function GetPid() {
  if [ x"$1" = x ];then
    return 99
  fi
  Pids=`ps -ef | grep $1 | grep -v grep | grep -v PPID | awk '{ print $2}'`
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then #命令执行成功
    echo $Pids              #以字符串形式返回Pids
  else
    return 99
  fi
}

#status功能：检查每一个进程的运行是否正常,并打印提示信息.
if [ "$1" = "status" ] ; then
  ProcNumber_myapp=`GetProcNum "$PROC_MYAPP"`
  if [ $ProcNumber_myapp -eq 0 ];then
    echo "myapp service not start...$usage_str!"
  else
    echo "myapp service started!"
  fi
fi

#需要将所有的依赖程序都启动,在watchdog中已做了处理,
start() {
  #运行所有进程
  printf "Starting service myapp..."
  #添加环境变量
  if [ "$MYAPP_LANG"x != "en_US.UTF-8"x ];then
    LANG="en_US.UTF-8"
    export LANG
  else
    export MYAPP_LANG
  fi
  
  #执行GetProcNum函数,传递参数$PROC_WATCHDOG.并将返回值赋值给ProcNum_watchdog.
  #只要watchdog起着,其他进程都起着.如果watchdog起着，提示服务已经起来了.
  ProcNum_watchdog=`GetProcNum "$PROC_WATCHDOG"`
  RETVAL=$?
  if [ $RETVAL -ne 0 ];then
    failure
    echo
    return $RETVAL
  fi
  if [ $ProcNum_watchdog -ne 0 ];then  #watchdog进程在运行.
    echo
    echo "service myapp already started!"
    return $RETVAL
  fi

  #$PROC_WATCHDOG 2>/dev/null >/dev/null & #后台运行,不在终端上显示打印信息.
  #1标准输出,2标准错误,&1等同于1
  $PROC_WATCHDOG >/dev/null 2>&1 & #同上
  
  Pnum_watchdog=`GetProcNum "$PROC_WATCHDOG"`
  RETVAL=$?
  if [ $RETVAL -ne 0 ];then
    failure
    echo
    return $RETVAL
  fi
  #如果
  if [ $Pnum_watchdog -eq 0 ];then
    echo "service myapp start failed! please check:"
    #检查看门狗是否有可执行权限
    echo "1.myapp_watchdog has the executable permissions."
    #检查看门狗是否放到了正确的路径
    echo "2.myapp_watchdog in the right path."

    echo ""
    return 99
  fi
  
  success
  echo
  
  RETVAL=0
  return $RETVAL
}

#需要将所有的依赖程序都关闭掉,如果有顺序需要注意顺序
stop() {
  #查看所有运行的程序并关闭掉.
  printf "Stopping service myapp..."

  #kill watchdog
  watch_dog=`GetPid "$PROC_WATCHDOG"`
  RETVAL=$?
  if [ $RETVAL -ne 0 ];then
    failure
    echo
    return $RETVAL
  fi
  for i in $watch_dog
  do
    kill -9 $i
  done

  #kill myapp
  my_app=`GetPid "$PROC_MYAPP"`
  RETVAL=$?
  if [ $RETVAL -ne 0 ];then
    failure
    echo
    return $RETVAL
  fi
  for i in $my_app
  do
    kill -9 $i
  done
  
  success
  echo

  RETVAL=0
  return $RETVAL
}

#start stop
restart() {
  
  stop
  start

  RETVAL=0
  return $RETVAL
}

#see how we were called.
case $1 in
  start):
    start
    ;;
  stop):
    stop
    ;;
  restart):
    restart
    ;;
  *)
    #用户使用提示信息
    echo $"Usage: service myapp {start|stop|restart|status}"
    RETVAL=3

esac

exit $RETVAL
